//
//  Daishou.m
//  TengyunStation
//
//  Created by Johnny on 15/10/27.
//  Copyright © 2015年 xingrong. All rights reserved.
//

#import "Daishou.h"
#import "SocketMethod.h"

@implementation Daishou

- (id)initWithString:(NSString *)string {
    self = [super init];
    if (self) {
        NSArray *array = [string componentsSeparatedByString:@","];
        int i = 0;
        self.companyId = [SocketMethod subArray:array Index:i ++];
        self.companyName = [SocketMethod subArray:array Index:i ++];
        self.companyLogo = [SocketMethod subArray:array Index:i ++];
        self.expressCount = [[SocketMethod subArray:array Index:i ++] intValue];
        NSString *receiveTime = [SocketMethod subArray:array Index:i ++];
        [self handleReceiveTime:receiveTime];
        self.receiveTime = [SocketMethod dateStringWithDateString:receiveTime];
        if (self.receiveTime.length > 11) {
            self.showTime = [self.receiveTime substringFromIndex:11];
        }
        self.batchNo = [SocketMethod subArray:array Index:i ++];
        self.expresserId = [SocketMethod subArray:array Index:i ++];
        self.expresserName = [SocketMethod subArray:array Index:i ++];
        self.expresserTelephone = [SocketMethod subArray:array Index:i ++];
        self.expressType = [SocketMethod subArray:array Index:i ++];
        self.handleType = [[SocketMethod subArray:array Index:i ++] intValue];
    }
    return self;
}

- (void)handleReceiveTime:(NSString *)receiveTime {
    self.receiveTime = [SocketMethod dateStringWithDateString:receiveTime];
    if (receiveTime.length >= 8) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"yyyyMMdd";
        NSDate *date = [dateFormatter dateFromString:[receiveTime substringToIndex:8]];
        NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *componets = [calendar components:NSDayCalendarUnit fromDate:date toDate:[NSDate date] options:NSCalendarWrapComponents];
        if (componets.day <= 0) {
            self.showDay = @"今天";
        } else if (componets.day <= 1) {
            self.showDay = @"昨天";
        } else {
            NSDateFormatter *showFormatter = [[NSDateFormatter alloc] init];
            showFormatter.dateFormat = @"yyyy-MM-dd";
            self.showDay = [showFormatter stringFromDate:date];
        }
    }
}

@end
